<?php
class YYKTpl{
	public static function complie($tplName, $param){
		$tplCache = YYK::$APP_path . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . YYK::$ctrlName . DIRECTORY_SEPARATOR . md5_file($tplName) . '.php';
		$tplFile = file_get_contents($tplName);

		//preg_match('/<include_once(\s){1,}file=[\'|\"](.*)[\'|\"](\s){0,}\/>/', $tplFile, $match);
		/*
		$tplFile = preg_replace('/<include_once(\s){1,}file=[\'|\"](.*)[\'|\"](\s){0,}\/>/', '<?php include "$2";?>', $tplFile);
		*/


		while(preg_match('/<include_once(\s){1,}file=[\'|\"](.*)[\'|\"](\s){0,}\/>/', $tplFile, $match)){
			$tmp = file_get_contents($match[2]);
			$tplFile = str_replace($match[0], $tmp, $tplFile);
			unset($tmp);
		}

		//preg_match('/<include(\s){1,}file=[\'|\"](.*)[\'|\"](\s){0,}\/>/', $tplFile, $match);
		/*
		$tplFile = preg_replace('/<include(\s){1,}file=[\'|\"](.*)[\'|\"](\s){0,}\/>/', '<?php include "$2";?>', $tplFile);
		*/
		while(preg_match('/<include(\s){1,}file=[\'|\"](.*)[\'|\"](\s){0,}\/>/', $tplFile, $match)){
			$tmp = file_get_contents($match[2]);
			$tplFile = str_replace($match[0], $tmp, $tplFile);
			unset($tmp);
		}
		
		//foreach
		$k = 0;
		while(preg_match('/<foreach[\s\S]{'.$k.',}?>/', $tplFile, $match)){
			if($xml = @simplexml_load_string('<tpl>' . $match[0] . '</foreach></tpl>')){
				$k = 0;
			}
			else{
				$k=strlen($match[0])-8 ;continue;
			}

			$xml = (array)($xml->foreach->attributes());
			if (isset($xml['@attributes'])){
				$xml = array_change_key_case($xml['@attributes']);
				extract($xml, EXTR_OVERWRITE);
			}

			$str = '<?php foreach (';
			if (isset($var) ){
				$str .= '$' . $var;
			}
			$str .= ' as ';
			if (isset($key)){
				$str .= '$' . $key . '=> ';
			}

			if (isset($val) ){
				$str .= '$' . $val;
			}
			$str .= '){?>' . "\n";
			//$tplFile = preg_replace('/<foreach'.$string[1].$string[2].'>/', $str , $tplFile);
			$tplFile = str_replace($match[0], $str, $tplFile);
			unset($var);unset($key);unset($val);
		}
		$tplFile = str_replace('</foreach>', '<?php }?>', $tplFile);


		//for
		$k = 0;
		while(preg_match('/<for[\s][\s\S]{'.$k.',}?>/', $tplFile, $match)){
			if($xml = @simplexml_load_string('<tpl>' . $match[0] . '</for></tpl>')){
				$k = 0;
			}
			else{
				$k=strlen($match[0])-4;continue;
			}

			$xml = (array)($xml->for->attributes());
			if (isset($xml['@attributes'])){
				$xml = array_change_key_case($xml['@attributes']);
				extract($xml, EXTR_OVERWRITE);
				$condition = preg_replace(
					array('/\s(eq)\s/','/\s(lt)\s/','/\s(gt)\s/','/\s(elt)\s/','/\s(egt)\s/',),
					array('==', '<','>','<=','>='), 
					$condition
				);
			}

			$str = '<?php for (';
			if (isset($var) && isset($init)){
				$str .= '$' . $var . '=' . $init;
			}
			$str .= ';';
			if (isset($condition)){
				$str .= $condition;
			}
			$str .= ';';
			if (isset($var) && isset($step)){
				$str .= '$' . $var . '+=' . $step;
			}
			$str .= '){?>' . "\n";
			$tplFile = str_replace($match[0], $str, $tplFile);
			unset($var);unset($init);unset($condition);unset($step);
		}
		$tplFile = str_replace('</for>', '<?php }?>', $tplFile);


		//if
		$k = 0;
		while(preg_match('/<if[\s][\s\S]{'.$k.',}?>/', $tplFile, $match)){
			//var_dump($match);
			if($xml = @simplexml_load_string('<tpl>' . $match[0] . '</if></tpl>')){
				$k = 0;
			}
			else{
				$k=strlen($match[0])-3; continue;
			}

			$xml = (array)($xml->if->attributes());
			if (isset($xml['@attributes'])){
				$xml = array_change_key_case($xml['@attributes']);
				extract($xml, EXTR_OVERWRITE);
				$condition = preg_replace(
							array('/\s(eq)\s/','/\s(lt)\s/','/\s(gt)\s/','/\s(elt)\s/','/\s(egt)\s/',),
							array('==', '<','>','<=','>='), 
							$condition
						);
			}

			$str = '<?php if ('.$condition.'){?>' . "\n";
			$tplFile = str_replace($match[0], $str, $tplFile);
			unset($var);unset($init);unset($condition);unset($step);
		}
		$tplFile = str_replace('<else/>', '<?php }else{?>', $tplFile);
		$tplFile = str_replace('</if>', '<?php }?>', $tplFile);


		//switch
		$k = 0;
		while(preg_match('/<switch[\s\S]{'.$k.',}?>/', $tplFile, $match)){
			if($xml = @simplexml_load_string('<tpl>' . $match[0] . '</switch></tpl>')){
				$k = 0;
			}
			else{
				$k=strlen($match[0])-7; continue;
			}

			$xml = (array)($xml->switch->attributes());
			if (isset($xml['@attributes'])){
				$xml = array_change_key_case($xml['@attributes']);
				extract($xml, EXTR_OVERWRITE);
			}

			$str = '<?php switch ('.$var.'){?>' . "\n";
			$tplFile = str_replace($match[0], $str, $tplFile);
			unset($var);unset($init);unset($condition);unset($step);
		}
		$tplFile = str_replace('</switch>', '<?php }?>', $tplFile);


		//case
		$k = 0;
		while(preg_match('/(<case[\s\S]*?>)[\s\S]{'.$k.',}?<\/case>/', $tplFile, $match)){
			//var_dump($match);
			if($xml = simplexml_load_string('<tpl>' . $match[0] . '</tpl>')){
				//echo '--' . $k . '--';
				$k = 0;
				$pos = strpos($tplFile, $match[0]) + strlen($match[0]) - 7 ;
			}
			else{
				$k=strlen($match[0])-13;
				// $k++;
				continue;
			}

			$xml = (array)($xml->case->attributes());
			if (isset($xml['@attributes'])){
				$xml = array_change_key_case($xml['@attributes']);
				extract($xml, EXTR_OVERWRITE);
			}

			if (isset($break) && $break==0){
				$tplFile = substr($tplFile, 0, $pos) . substr($tplFile, $pos+7);
			}
			else{
				$tplFile = substr($tplFile, 0, $pos) . '<?php break;?>' . substr($tplFile, $pos+7);
			}

			if (isset($val)){
				$str = '<?php case '.$val.': ?>' . "\n";
			}
			else{
				$str = '<?php case "": ?>';
			}
				$pos = strpos($tplFile, $match[1]) + strlen($match[1]);
				$tplFile = str_replace($match[1], $str, substr($tplFile, 0, $pos) ) . substr($tplFile, $pos);

			// file_put_contents(YYK::$APP_path . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . YYK::$ctrlName . DIRECTORY_SEPARATOR.$fm.'.html', 
			// 	$match[0] . "\n\n\n\n" . $tplFile);
			// $fm++;
			unset($val);unset($break);
		}


		//default
		$k = 0;
		while(preg_match('/<defalut[\s]{'.$k.',}?\/>/', $tplFile, $match)){
			if($xml = @simplexml_load_string('<tpl>' . $match[0] . '</tpl>')){
				$k = 0;
			}
			else{
				$k=strlen($match[0])-8; continue;
			}

			$str = '<?php default : ?>' . "\n";
			$tplFile = str_replace($match[0], $str, $tplFile);
		}


		//简单php语句
		$k = 0;
		while(preg_match('/\{:[\s\S]{'.$k.',}?\}/', $tplFile, $match)){
			//var_dump($k);
			//var_dump($match);
			$tmp = str_replace('\\\"', '  ', $match[0]);
			$tmp = str_replace('\\\'', '  ', $tmp);
			//var_dump($tmp);
			if(substr_count($tmp, '"')%2 == 0 && substr_count($tmp, "'")%2 == 0 )   {
				$k = 0;
			}
			else{
				$k =strlen($match[0]);continue;
			}
			$tmp = trim($match[0]);
			$tmp = trim($tmp, '{}');
			$tmp = trim($tmp, ':');
			$tmp = trim($tmp);
			if (substr($tmp,0,1)=='='){
				$tmp = trim($tmp, '=');
				$tmp = '<?php echo ' . $tmp . ';?>';
			}
			else{
				$tmp = '<?php ' . $tmp . ';?>';
			}
			$tplFile = str_replace($match[0], $tmp, $tplFile);
		}
		//var_dump($param);
		//extract($param, EXTR_OVERWRITE);
		//变量替换
		$k = 0;
		while(preg_match('/\{\$[\S]{1,}?\}/', $tplFile, $match)){
			//var_dump($k);
			//var_dump($match);

			$tmp = trim($match[0], '{}');
			$tmp = '<?php echo ' . $tmp . ';?>';
			$tplFile = str_replace($match[0], $tmp, $tplFile);
		}

		//常量替换
/*		$offset= 0;
		while(preg_match('/__([a-zA-Z0-9_-])+__/', $tplFile, $match,PREG_OFFSET_CAPTURE,$offset)){
			$s3 = '<?php echo YYK::$config["tplConst"]["' . $match[0][0] . '"];?>';
			$offset = $match[1][1] + strlen($s3);
			var_dump($match);
			echo $offset. " --- ";
			$tmp = isset(YYK::$config['tplConst'][$match[0][0]]) ? $s3 : '';// YYK::$config['tplConst'][$match[0]]
			echo $tmp . "<br>";
			$tplFile = str_replace($match[0][0], $tmp, $tplFile);
			break;
		}*/
		$tplFile = preg_replace('/\?>(\s){0,}<\?php/', '', $tplFile);
		// $tplFile='';
		
		foreach (YYK::$config['tplConst'] as $key => $value) {
			$tplFile = str_replace($key,'<?php echo YYK::$config["tplConst"]["'.$key.'"];?>',$tplFile);
		}

		if (!file_exists(YYK::$APP_path . DIRECTORY_SEPARATOR . 'cache')) {
			mkdir(YYK::$APP_path . DIRECTORY_SEPARATOR . 'cache');
		}
		if (!file_exists(YYK::$APP_path . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . YYK::$ctrlName)) {
			mkdir(YYK::$APP_path . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . YYK::$ctrlName);
		}
		file_put_contents($tplCache, $tplFile);
	}
}

/*
{$param['xx']}											<?php echo $param['x'];?>
{$param.xx}
{:iconv('gbk','utf8',$char)}							<?php iconv('gbk','utf8',$char);?>
{:=iconv('gbk','utf8',$char)}							<?php echo iconv('gbk','utf8',$char);?>
<for var=i init=x condition=true step=1></for>			<?php for ($i=x;condition;$i+=1){?>  <?php }?>


<foreach var=arr key=k val=item></foreach>				<?php foreach($arr as $k => $item){?><?php }?>

<if condition=true></if>								<?php if (condition){?><?php}?>

<switch var=x>											<?php switch($x){?>
	<case val=n break=1></case>								<?php case n:?>  <?php ;?>
	<default>												<?php default:?>
</switch>												<?php }?>

<include file='filename.php'/>							<?php include 'filename.php';?>


*/